import axios from 'axios'

import { setRSApass, getRSApass } from './JSEncrypt'
import { getToken, userSn } from './APPempower'
// create an axios instance
const service = axios.create({
  // headers: { 'Content-Type': 'application/json;charset=UTF-8' }
})

// request interceptor
service.interceptors.request.use(
  config => {
    if (config.data && !config.data.noMember) {
      config.data.token = getToken()
      config.data.userSn = userSn()
    }
    if (config.data && !config.data.No_RSA) {
      setConsole(JSON.stringify(config.data))
      config.data = JSON.stringify({ "data": setRSApass(JSON.stringify(config.data)) })
      setConsole(config.data)
    } else {
      config.data = JSON.stringify(config.data)
    }
    return config
  },
  error => {
    // do something with request error
    return Promise.reject(error)
  }
)
function setConsole(data) {
  if (process.env.NODE_ENV !== 'production') {
    console.log(data)
  }
}

export default service
